package tgc.edu.yzy.web.controller;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.data.domain.Sort.Direction;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContext;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import tgc.edu.yzy.custom.Custom;
import tgc.edu.yzy.custom.JpgridUtils;
import tgc.edu.yzy.custom.JqgridParam;
import tgc.edu.yzy.entity.EimAdmin;
import tgc.edu.yzy.entity.EimInvoice;
import tgc.edu.yzy.entity.EimRegister;
import tgc.edu.yzy.entity.EimUser;
import tgc.edu.yzy.security.User2;
import tgc.edu.yzy.service.AdminService;
import tgc.edu.yzy.service.InvoiceService;
import tgc.edu.yzy.service.UserService;
import tgc.edu.yzy.web.form.InvoiceForm;

@Controller
@RequestMapping(value="/user")
public class UserController {
	@Autowired
	private AdminService admin; 
	@Autowired
	private InvoiceService inv;
	@Autowired
	private UserService userservice;
	@Autowired
	private Custom custom;
	/***
	 * 跳转到发票页面
	 * @return
	 */
	@RequestMapping(value="/userinvoice")
	public String userinvoice() {
		return "home/views/userinvoice";
	}
	/****
	 * 显示用户管理的josn数据
	 * @param param
	 * @return
	 */
	@RequestMapping(value="/userinvoicelist")
	@ResponseBody
	private Object userinvoicelist(JqgridParam param) {
		Pageable pageable = param.buildPageable();
		Page<EimInvoice> page=null;
		
		SecurityContext ctx = SecurityContextHolder.getContext();
		Authentication auth = ctx.getAuthentication();
		User2 user = (User2) auth.getPrincipal();
		EimUser users=userservice.findById(user.getId());
		if(!param.getSearch()) {
			page=inv.findByEimadmin(pageable, users.getEimadmins());
		}else {
			String q=(param.getFilters()).substring(0,(param.getFilters()).length()-2);
			q=q+",{\"field\":\"eimadminids\",\"op\":\"eq\",\"data\":\""+users.getEimadmins().getId()+"\"}]}";
			
			param.setFilters(q); 
			page=inv.findAll(param.buildSpec(), pageable);
		}
		
		HashMap<String , Object> result = JpgridUtils.getPageResult(page);
		return result;
	}
	/***
	 * load 的页面
	 * @return
	 */
	@RequestMapping(value="form")
	private String form() {
		return "home/views/form";
	}
	//判断发票数据里是否有这个 报销号码
	@RequestMapping(value="numberxy")
	@ResponseBody
	private Boolean numberxy(String number) {
		EimInvoice a=inv.findByNumber(number);
		if(a!=null) {
			return false;
		}else {
			return true;
		}
	}
	//用户新增发票
	@RequestMapping(value="/add")
	private String a(HttpServletRequest request,InvoiceForm from) {
		EimInvoice ins=new EimInvoice();
		BeanUtils.copyProperties(from, ins);
		
		Date date=new Date(); 
		SimpleDateFormat sd= new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		
		ins.setMindate(from.getMindate());
		ins.setMaxdate(sd.format(date));
		EimUser eimuser = userservice.findById(custom.getUser2());
		ins.setEimuser(eimuser);
		ins.setEimadminids(eimuser.getEimadminids());
		ins.setEimadmin(eimuser.getEimadmins());
		inv.save(ins);
		EimAdmin adminuuid=admin.findById(eimuser.getEimadmins().getId());
		Integer as=adminuuid.getManaged();
		Integer ab=as+1;
		adminuuid.setManaged(ab);	
		admin.save(adminuuid);
		//做记录 到日志表里面
		custom.record(request, "发票", eimuser.getName()+"新增了一张电子发票"+from.getNumber());
		return "home/views/form";
	}
	
	/***
	 * 用户权限新增以后跳转的页面
	 * @param map
	 * @param pagesize
	 * @return
	 */
	@RequestMapping(value="/eiminvoice")
	private String userpower(ModelMap map,String pagesize) {
		EimUser eimuser = userservice.findById(custom.getUser2());
		if("1".equals(eimuser.getQl())) {
			Integer pagesize1;
			try {
				pagesize1=Integer.parseInt(pagesize);
			}catch (Exception e) {
				pagesize1=10;
			}
			if(pagesize1<=0||pagesize1==null) {
				pagesize1=10;
			}
			map.put("pagesize",pagesize1);
			return "home/views/eiminvoice";
		}else {
			return "redirect:logout";
		}
	}
	
		//获取数据长度
		@RequestMapping(value="/userpage")
		@ResponseBody
		public Object userpage() {
			long uu=inv.countByEimadmin(userservice.findById(custom.getUser2()).getEimadmins());
			return uu;
		}
		//ajax提交获取每页的数据
		@RequestMapping(value="/userinlist")
		@ResponseBody
		private Object powerlist(ModelMap map,Integer pageindex,Integer pagesize) {
			Sort sort = new Sort(Direction.DESC, "id");
			Pageable pageable=PageRequest.of((pageindex-1), pagesize,sort);
			Page<EimInvoice> users=inv.findByEimadmin(pageable,userservice.findById(custom.getUser2()).getEimadmins());
			return users;
		}
		
		@RequestMapping(value="/userremove")
		private String userremove(HttpServletRequest request,Integer id) {
			EimUser eimuser = userservice.findById(custom.getUser2());
			if("1".equals(eimuser.getQl())) {
				custom.record(request, "删除发票", eimuser.getName()+"删除了一张发票");
				inv.deleteByID(id);
				EimAdmin eim=admin.findById(eimuser.getEimadmins().getId());
				Integer mstsc=eim.getManaged()-1;
				eim.setManaged(mstsc);
				admin.save(eim);
			}
			return "redirect:eiminvoice";
		}
		
		
		
}

